<?php
/**
 * Created by PhpStorm.
 * User: 小夏
 * name:课程管理
 * Date: 2016/9/27 0027
 * Time: 13:59
 */
namespace Invest\Controller;
use Common\Controller\AdminbaseController;
class AdminCourseController extends AdminbaseController {
    protected $course;
    function _initialize() {
        parent::_initialize();
        $this->course = M("Course");
    }
    function index(){
        $where = array('delete_type'=>'0');
        $classify=I('request.classify');
        $name=I('request.name');
        if(!empty($classify)){
            $where['classify']=$classify;
        }
        if(!empty($name)){
            $where['name']=array('like',"%$name%");
        }
        $count = $this->course->where($where)->count();// 查询满足要求的总记录数
        $Page  = $this->Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $this->course->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if($list){
            $teacheerids = array();
            foreach ($list as $id){
               // $teacheerid[]=$id["uid"];
                array_push($teacheerids,$id['uid']);
            }

          //  $teacheerids = join(",", array_filter($teacheerid));
            $teaname= M('Users')->where(array('id'=>array('in',$teacheerids)))->getField('id,user_nicename',true);
            foreach ($list as $key=>$value){
                $list[$key]['teaname'] = $teaname[$value['uid']];
            }
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("page", $Page->show('Admin'));
        $this->display();
    }
    public function add(){

        $teacher = M('Users')->field('id as id,user_nicename as text')->where(array('user_type'=>3))->select();
        $this->assign('cheshi',json_encode($teacher));
        $this->display();
    }
    public function add_post(){
        if (IS_POST) {

            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);

            $_POST['post']['post_author']=get_current_admin_id();
            $content=I("post.post");
            $content['smeta']=json_encode($_POST['smeta']);
            $content['post_date']=strtotime($content['post_date']);
            $content['post_content']=htmlspecialchars_decode($content['post_content']);

            $result=$this->course->fetchSql(false)->add($content);

            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }
        }
    }
    public function edit(){
        $id=  intval(I("get.id"));

        $teacher = M('Users')->field('id as id,user_nicename as text')->where(array('user_type'=>3))->select();
        $this->assign('cheshi',json_encode($teacher));

        $post=$this->course->where("id=$id")->find();
        $this->assign("post",$post);
        $this->assign("smeta",json_decode($post['smeta'],true));

        $this->display();
    }

    public function edit_post(){
        if (IS_POST) {
            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);
            unset($_POST['post']['post_author']);
            $content=I("post.post");
            $content['smeta']=json_encode($_POST['smeta']);
            $content['post_date']=strtotime($content['post_date']);
            $content['post_content']=htmlspecialchars_decode($content['post_content']);
            $result=$this->course->save($content);
            if ($result!==false) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }
        }
    }
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->course->where(array('id'=>$id))->setField('delete_type','1') !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }
    public function recycle(){
        $where = array('delete_type'=>'1');
        $classify=I('request.classify');
        $name=I('request.name');
        if(!empty($classify)){
            $where['classify']=$classify;
        }
        if(!empty($name)){
            $where['name']=array('like',"%$name%");
        }
        $count = $this->course->where($where)->count();// 查询满足要求的总记录数
        $Page  = $this->Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $this->course->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if($list){
            $teacheerids =array();
            foreach ($list as $id){
                array_push($teacheerids,$id['uid']);
               // $teacheerid[]=$id["uid"];
            }
            //$teacheerids = join(",", array_filter($teacheerid));
            $teaname= M('Users')->where(array('id'=>array('in',$teacheerids)))->getField('id,user_nicename',true);
            foreach ($list as $key=>$value){
                $list[$key]['teaname'] = $teaname[$value['uid']];
            }
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("page", $Page->show('Admin'));
        $this->display();
    }
    public function restore(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->course->where(array('id'=>$id))->setField('delete_type','0') !==false) {
                $this->success("还原成功！");
            } else {
                $this->error("还原失败！");
            }
        }
    }
}